package com.starview.star.operators.uri.runtime;

import com.starview.platform.data.EvaluationException;
import com.starview.platform.data.IFunction;
import com.starview.platform.data.IValue;
import com.starview.platform.data.type.IType;
import com.starview.platform.data.type.StandardTypes;
import com.starview.platform.data.type.TypeUtils;
import com.starview.platform.data.value.Factory;
import com.starview.platform.data.value.ResourceURI;
import com.starview.platform.resource.ResourceException;
import com.starview.platform.resource.Resources;
import com.starview.star.operators.CafeEnter;

/**
 * Get the content of a URI as a string.
 * 
 * @author fgm
 * 
 */
public class GetURI implements IFunction
{

  @CafeEnter
  public static String enter(ResourceURI uri) throws EvaluationException
  {
    try {
      return Resources.getUriContent(uri);
    } catch (ResourceException e) {
      throw new EvaluationException(e.getMessage());
    }
  }

  @Override
  public IValue enter(IValue... args) throws EvaluationException
  {
    return Factory.newString(enter((ResourceURI) args[0]));
  }

  @Override
  public IType getType()
  {
    return type();
  }

  public static IType type()
  {
    return TypeUtils.functionType(ResourceURI.type, StandardTypes.rawStringType);
  }

}
